﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class RubberDropper : BaseObj
{
    public GameObject m_Target;
    public Transform m_FatherTarget;
    public Transform m_BigFather;
    public Dropper m_Dropper;
    public GameObject m_WaterColumn;
    public GameObject DropsOfWater;
    public Transform DropsOfWaterTarget;

    bool OnPlatfrom;
    bool HaveFtWater;//里面有了酚酞溶液

    public void Start()
    {
        base.Start();
        m_rigidbody.useGravity = false;
        m_rigidbody.isKinematic = true;
        m_collider.isTrigger = true;
        transform.rotation = Quaternion.Euler(0, 0, 0);
        transform.position = m_Target.transform.position + new Vector3(0, 0.02f, 0);
        transform.SetParent(m_FatherTarget);
    }

    public override void OnReleseTrigger(NoloDeviceType handleType)
    {
        if (!OnPlatfrom)
        {
            base.OnReleseTrigger(handleType);
            transform.SetParent(m_BigFather);
        }
        else
        {
            isHandle = false;
            OnBottle();
        }
    }

    public void OnBottle()
    {
        m_rigidbody.useGravity = false;
        m_rigidbody.isKinematic = true;
        transform.rotation = Quaternion.Euler(0, 0, 0);
        transform.position = m_Target.transform.position + new Vector3(0, 0.02f, 0);
        transform.SetParent(m_FatherTarget);
    }

    private void Update()
    {
        if (isHandle)
        {
            if (NoloVR.NoloVRInput.singleton.OnNoloLeftTouchDown() || NoloVR.NoloVRInput.singleton.OnNoloRightTouchDown())
            {
                if (m_Dropper.InFTWater && !HaveFtWater)
                {
                    GetFtWater();
                }
                else if (HaveFtWater)
                {
                    Drop();
                }
            }
        }
    }

    public void Drop()
    {
        GameObject drop = Instantiate(DropsOfWater, DropsOfWaterTarget);
        drop.transform.SetParent(m_BigFather.transform);
        m_WaterColumn.SetActive(false);
        HaveFtWater = false;
    }

    public void GetFtWater()
    {
        HaveFtWater = true;
        m_WaterColumn.SetActive(true);
    }

    public void SetStartMove()
    {
        transform.SetParent(m_BigFather);
        m_rigidbody.useGravity = false;
        m_rigidbody.isKinematic = true;
    }

    private void LateUpdate()
    {
        if (Vector3.Distance(m_Target.transform.position, transform.position) < 0.1f)
        {
            OnPlatfrom = true;
        }
        else
        {
            OnPlatfrom = false;
        }
    }

    //记录每个大步骤的初始状态
    #region
    public void SetBigStepTwo()
    {
        Start();
    }

    public void SetBigStepThree()
    {
        OnBottle();
    }

    public void SetBigStepFour()
    {
        OnBottle();
    }

    public void SetBigStepFive()
    {

    }
    #endregion

    public void ResetParent()
    {
        transform.SetParent(m_BigFather);
    }

    public void OnGround()
    {
        m_rigidbody.useGravity = true;
        m_rigidbody.isKinematic = false;
        m_collider.isTrigger = false;
    }

}
